package com.mtons.mblog.modules.service.impl;

import com.mtons.mblog.base.utils.CommonUtils;
import com.mtons.mblog.modules.entity.Channel;
import com.mtons.mblog.modules.entity.LearnContentType;
import com.mtons.mblog.modules.repository.LearnContentTypeRepository;
import com.mtons.mblog.modules.service.LearnContentTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.*;

/**
@author Tison
*/
@Service
public class LearnContentTypeServiceImpl  implements LearnContentTypeService  {

	@Resource
	private LearnContentTypeRepository contentTypeRepository;

	public Page<LearnContentType> paging(Pageable pageable, String name) {
		Page<LearnContentType> page = contentTypeRepository.findAll((root, query, builder) -> {
			Predicate predicate = builder.conjunction();

			if (StringUtils.isNoneBlank(name)) {
				predicate.getExpressions().add(
						builder.like(root.get("name"), "%" + name + "%"));
			}

			query.orderBy(builder.desc(root.get("updateTime")));
			return predicate;
		}, pageable);
		return page;
	}

	@Transactional
	@Override
	public void update(LearnContentType learnContentType) {

		Date nowDate = new Date();
		if(learnContentType.getId() == null){
			learnContentType.setId(CommonUtils.getUUID());
			learnContentType.setCreateTime(nowDate);
			contentTypeRepository.save(learnContentType);
		}else {
			Optional<LearnContentType> optional = contentTypeRepository.findById(learnContentType.getId());
			LearnContentType po = optional.orElse(new LearnContentType());
			BeanUtils.copyProperties(learnContentType, po);
			po.setUpdateTime(nowDate);
			contentTypeRepository.save(po);
		}

	}

	@Override
	public void delete(String id) {
		contentTypeRepository.deleteById(id);
	}

	public LearnContentType save(LearnContentType obj) {
		 return contentTypeRepository.save(obj);
	 }


	 @Transactional
	 public List<LearnContentType> saveAll(Iterable<LearnContentType> list) {
		 return contentTypeRepository.saveAll(list);
	 }


	 public LearnContentType getOne(String id) {
		 return contentTypeRepository.getOne(id);
	 }


	 public LearnContentType findById(String id) {
		 Optional<LearnContentType> obj = contentTypeRepository.findById(id);
		 return obj.orElse(null);
	 }


	 public void deleteById(String id) {
		 contentTypeRepository.deleteById(id);
	 }


	 @Transactional
	 public void deleteAll(List list) {
		 contentTypeRepository.deleteAll(list);
	 }


	 public void delete(LearnContentType obj) {
		 contentTypeRepository.delete(obj);
	 }


	 public boolean existsById(String id) {
		 return contentTypeRepository.existsById(id);
	 }


	 public long count() {
		 return contentTypeRepository.count();
	 }


	 public List<LearnContentType> findAll() {
		 return contentTypeRepository.findAll();
	 }


	 public List<LearnContentType> findAll(LearnContentType obj) {
		 List<LearnContentType> list = contentTypeRepository.findAll(Example.of(obj));
		 return list==null||list.size()<1?null:list;
	 }


	 public List<LearnContentType> findAll(Sort sort) {
		 return contentTypeRepository.findAll(sort);
	 }


	 public List<LearnContentType> findAllById(Iterable<String> ids) {
		 return contentTypeRepository.findAllById(ids);
	 }


	 public List<LearnContentType> findAll(Example<LearnContentType> e) {
		 return contentTypeRepository.findAll(e);
	 }


	 public List<LearnContentType> findAll(Example<LearnContentType> e, Sort sort) {
		 return contentTypeRepository.findAll(e,sort);
	 }


	 public Page<LearnContentType> findAll(Pageable page) {
		 return contentTypeRepository.findAll(page);
	 }


	 public Page<LearnContentType> findAll(Example<LearnContentType> e, Pageable page) {
		 return contentTypeRepository.findAll(e,page);
	 }


	 public Page<LearnContentType> findAll(LearnContentType obj, Pageable page) {
		 return contentTypeRepository.findAll(Example.of(obj),page);
	 }


}
